package com.qf.business.user.core.controller;

import com.qf.business.user.core.service.SysUserService;
import com.qf.commons.core.utils.QfBeanUtils;
import com.qf.commons.data.result.R;
import com.qf.data.user.dto.SysUserPowersDto;
import com.qf.data.user.entity.SysUser;
import com.qf.data.user.vo.input.SysSetUserRoleInput;
import com.qf.data.user.vo.input.SysUserInput;
import com.qf.data.user.vo.input.SysUserSearchInput;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 后台用户管理
 * author Ken
 * create_time 2022/8/31
 */
@RestController
@RequestMapping("/sys/user")
@Slf4j
public class UserController {

    @Autowired
    private SysUserService userService;

    /**
     * 查询后台用户列表
     * @return
     */
    @RequestMapping("/list")
    public R list(SysUserSearchInput searchInput){
        List<SysUser> users = userService.search(searchInput);
        return R.create(users);
    }

    /**
     * 新增用户
     * @return
     */
    @RequestMapping("/insert")
    public R insert(@Valid SysUserInput sysUserInput){
        log.debug("[insert user] 新增用户 - {}", sysUserInput);
        SysUser sysUser = QfBeanUtils.copyBean(sysUserInput, SysUser.class);
        userService.save(sysUser);
        return R.create("succ");
    }

    /**
     * 根据用户id 修改用户的角色列表
     * @return
     */
    @RequestMapping("/updateRoles")
    public R updateRoles(SysSetUserRoleInput userRoleInput){
        log.debug("[set user roles] 设置用户的角色 - {}", userRoleInput);
        userService.updateRoles(userRoleInput);
        return R.create(null);
    }


    /**
     * 根据用户名和密码查询后台用户的信息
     * @return
     */
    @RequestMapping("/queryUserByUn")
    public R<SysUserPowersDto> queryUserByUnAndPw(String username){
        log.debug("[query user byusername] 根据用户名查询用户信息以及权限信息 - {}", username);
        SysUserPowersDto sysUserPowersDto = userService.queryUserByUn(username);
        log.debug("[query user byusername] 查询结果 - {}", sysUserPowersDto);
        return R.create(sysUserPowersDto);
    }
}
